<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\controller\v1;

use app\api\model\Returnorder as ReturnorderModel;
use app\api\model\Member as MemberModel;
use app\api\model\Goods as GoodsModel;
use app\api\model\Address as AddressModel;

use app\api\model\Goodsorder as GoodsorderModel;


use think\Exception;
use think\Log;
use think\Request;
use think\Session;

class Returnorder
{
    /**
	*添加
	*/
	public function add(){
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']=Session::get('USER_KEY');
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			
			if(empty($all['oid'])){
				return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['orderpic'])){
				return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['productpic'])){
				return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['returncause'])){
				return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
			}
			
			$data_goodsorder = GoodsorderModel::selectOneGoodsByOid($all);
			if($data_goodsorder){
				//$data_goodsorders = $data_goodsorder->toArray();
				//$all['goodsname'] = $data_goodsorders['goodsname'];
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
			$data = ReturnorderModel::addOneReturnorder($all);
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
		
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);
		}
	}

	
	/**查询一条记录*/
	public function find($oid){
		$data = ReturnorderModel::selectOneReturnorderByOid($oid);
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	/**
     * 列表
     */
    public function lists()
    {
		
		$length = 10;
		
		$all = Request::instance()->param();
		
		$all['mid']=Session::get('USER_KEY');
		if(empty($all['mid'])){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
		
		if(empty($all['draw'])){ $draw = 1; }else{ $draw = trim($all['draw']); }
		
		$start=($draw-1)*$length;
		$where = ' 1 = 1 ';
		$where .= ' AND mid = ' . $all['mid'] . ' ';
		
		$list = ReturnorderModel::getListByWhere($where, $start, $length);
		$data_order = array();
		
		$all['oid'] = 0;
		foreach($list as $k=>$v){
			$all['oid'] = $v['oid'];
			$data_order = GoodsorderModel::selectOneGoodsByOid($all);
			$list[$k]['receive_name']=$data_order['receive_name'];
			$list[$k]['receive_tel']=$data_order['receive_tel'];
			$list[$k]['receive_addr']=$data_order['receive_addr'];
			$list[$k]['receive_beizhu']=$data_order['receive_beizhu'];
			$list[$k]['goodsname']=$data_order['goodsname'];
			$list[$k]['sigprice']=$data_order['sigprice'];
			$list[$k]['total']=$data_order['total'];
		}
		
		$count = ReturnorderModel::getCount($where);
		$data = array("returnCode"=>1,"returnMsg"=>"success!", 'recordsFiltered' => $count, 'draw' => $draw,'data' => $list);
		return json($data);
    }
	
}